﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Web.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using AnyRepMembershipProvider;
using AnyRepMembershipProvider.Tests.Mocks;

namespace AnyRepMembershipProvider.Tests
{
    /// <summary>
    /// Summary description for CompositeMembershipProvider
    /// </summary>
    [TestClass]
    public class AnyRepMembershipTests
    {
        private const string PROVIDER_NAME = "AnyRepMembershipProvider";

        private static Mock<IAnyRepMembershipConfig> configMock;

        [ClassInitialize]
        public static void TestClassInitializer(TestContext testContext)
        {
            // Initialize the shared Mock for Configuration Interface
            configMock = new Mock<IAnyRepMembershipConfig>();
            configMock.Setup(c => c.ApplicationName).Returns("/");
            configMock.Setup(c => c.Description).Returns(string.Empty);
            configMock.Setup(c => c.EnablePasswordReset).Returns(true);
            configMock.Setup(c => c.EnablePasswordRetrieval).Returns(false);
            configMock.Setup(c => c.MaxInvalidPasswordAttempts).Returns(5);
            configMock.Setup(c => c.MinRequiredNonAlphanumericCharacters).Returns(0);
            configMock.Setup(c => c.MinRequiredPasswordLength).Returns(6);
            configMock.Setup(c => c.PasswordAttemptWindow).Returns(10);
            configMock.Setup(c => c.PasswordFormat).Returns(MembershipPasswordFormat.Clear);
            configMock.Setup(c => c.PasswordStrengthRegularExpression).Returns(string.Empty);
            configMock.Setup(c => c.RepositoryAssemblyName).Returns("AnyRepMembershipProvider.Tests");
            configMock.Setup(c => c.RepositoryClassName).Returns("AnyRepMembershipProvider.Tests.Mocks.MockAnyRepMembershipRepository");
            configMock.Setup(c => c.RequiresQuestionAndAnswer).Returns(false);
            configMock.Setup(c => c.RequiresUniqueEmail).Returns(true);
        }

        [TestMethod]
        public void CreateUser_Should_Return_Null_And_Status_Should_Be_InvalidPassword_When_OnValidatePassword_Cancel()
        {
            AnyRepMembership membership = new AnyRepMembership(PROVIDER_NAME, configMock.Object, new MockMembershipUserHelpers());
            membership.ValidatingPassword += (s, e) => e.Cancel = true;

            MembershipCreateStatus membershipCreateStatus;

            Assert.IsNull(membership.CreateUser("rafael", "Admin@123", "rafael@ravero.net", "", "", true, Guid.NewGuid(), out membershipCreateStatus));
            Assert.AreEqual(MembershipCreateStatus.InvalidPassword, membershipCreateStatus);
        }

        [TestMethod]
        public void CreateUser_Should_Return_Null_And_Status_Should_Be_DuplicateEmail_When_EmailDuplicated()
        {
            AnyRepMembership membership = new AnyRepMembership(PROVIDER_NAME, configMock.Object, new MockMembershipUserHelpers());

            MembershipCreateStatus membershipCreateStatus;

            Assert.IsNull(membership.CreateUser("rafael", "Admin@123", "rafael@ravero.net", "", "", true, Guid.NewGuid(), out membershipCreateStatus));
            Assert.AreEqual(MembershipCreateStatus.DuplicateEmail, membershipCreateStatus);
        }

        [TestMethod]
        public void CreateUser_Should_Return_Null_And_Status_Should_Be_DuplicateUserName_When_UserNameDuplicated()
        {
            AnyRepMembership membership = new AnyRepMembership(PROVIDER_NAME, configMock.Object, new MockMembershipUserHelpers());

            MembershipCreateStatus membershipCreateStatus;

            Assert.IsNull(membership.CreateUser("rafael", "Admin@123", "rafael@improversoftware.com", "", "", true, Guid.NewGuid(), out membershipCreateStatus));
            Assert.AreEqual(MembershipCreateStatus.DuplicateUserName, membershipCreateStatus);
        }

        [TestMethod]
        public void CreateUser_Should_Create_A_New_User()
        {

        }
    }
}
